package com.ruoyi.userr.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.foodworks.mapper.LikesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.userr.mapper.UserLikesMapper;
import com.ruoyi.userr.domain.Likes;
import com.ruoyi.userr.service.ILikesService;

import javax.annotation.Resource;

/**
 * 作品点赞Service业务层处理
 * 
 * @author lpt
 * @date 2025-08-13
 */

@Service("userLikesServiceImpl")
public class LikesServiceImpl implements ILikesService 
{
    @Resource
    @Autowired
    private UserLikesMapper userLikesMapper;
    //private LikesMapper likesMapper;  // 匹配容器中的LikesMapper代理对象
    /**
     * 查询作品点赞
     * 
     * @param id 作品点赞主键
     * @return 作品点赞
     */
    @Override
    public Likes selectLikesById(Integer id)
    {
        return userLikesMapper.selectLikesById(id);
    }

    /**
     * 查询作品点赞列表
     * 
     * @param likes 作品点赞
     * @return 作品点赞
     */
    @Override
    public List<Likes> selectLikesList(Likes likes)
    {
        return userLikesMapper.selectLikesList(likes);
    }

    /**
     * 新增作品点赞
     * 
     * @param likes 作品点赞
     * @return 结果
     */
    @Override
    public int insertLikes(Likes likes)
    {
        likes.setCreateTime(DateUtils.getNowDate());
        return userLikesMapper.insertLikes(likes);
    }

    /**
     * 修改作品点赞
     * 
     * @param likes 作品点赞
     * @return 结果
     */
    @Override
    public int updateLikes(Likes likes)
    {
        return userLikesMapper.updateLikes(likes);
    }

    /**
     * 批量删除作品点赞
     * 
     * @param ids 需要删除的作品点赞主键
     * @return 结果
     */
    @Override
    public int deleteLikesByIds(Integer[] ids)
    {
        return userLikesMapper.deleteLikesByIds(ids);
    }

    /**
     * 删除作品点赞信息
     * 
     * @param id 作品点赞主键
     * @return 结果
     */
    @Override
    public int deleteLikesById(Integer id)
    {
        return userLikesMapper.deleteLikesById(id);
    }

    @Override
    public List<Map<String, Object>> getAuthorLikes(Long authorId) {
        if (authorId == null || authorId <= 0) {
            throw new IllegalArgumentException("作者ID无效");
        }
        // 直接返回Map列表，无需转换为VO
        return userLikesMapper.selectLikesByAuthorId(authorId);
    }

    // 新增方法：统计点赞总数
    @Override
    public Integer getAuthorLikeCount(Long authorId) {
        if (authorId == null || authorId <= 0) {
            throw new IllegalArgumentException("作者ID无效");
        }
        // 调用Mapper统计数量，若没有数据则返回0（MyBatis会将COUNT(*)的空结果转为0）
        Integer count = userLikesMapper.countLikesByAuthorId(authorId);
        return count == null ? 0 : count;
    }
}
